package com.PlantVsZombies.game;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.PlantVsZombies.constant.GameVariable;
import com.PlantVsZombies.ui.GamePanel;
import com.PlantVsZombies.ui.ServerPanel;
/**
 * 联机对战选择界面
 * @author wangdongdong
 *
 */
public class OnlinePanel extends JPanel{

	private JButton jButton0;
	private JButton jButton1;
	private JRadioButton jRadioButton0;
	private JRadioButton jRadioButton1;
	private JLabel jLabel0;
	private JLabel jLabel1;
	private JTextField jTextField0;
	private JTextField jTextField1;
	private JButton jButton3;
	private JFrame frame;

	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.setText("jButton0");
		}
		return jButton0;
	}

	private void initComponents() {
		setLayout(null);
		add(getJButton0());
		add(getJLabel0());
		add(getJLabel1());
		add(getJTextField0());
		add(getJTextField1());
		add(getJRadioButton1());
		add(getJRadioButton0());
		add(getJButton1());
		add(getJButton3());
		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(jRadioButton0);
		buttonGroup.add(jRadioButton1);
		setSize(319, 185);
	}

	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setText("取消");
			jButton3.setBounds(196, 134, 73, 28);
		}
		return jButton3;
	}

	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setBounds(101, 90, 171, 22);
			jTextField1.setText("8888");
			jTextField1.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					jTextField1.setText(null);
				}
			});
		}
		return jTextField1;
	}

	private JTextField getJTextField0() {
		if (jTextField0 == null) {
			jTextField0 = new JTextField();
			jTextField0.setBounds(101, 57, 171, 22);
			jTextField0.setText("127.0.0.1");
			jTextField0.setEnabled(false);
		}
		return jTextField0;
	}

	private JLabel getJLabel1() {
		if (jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("端口号：");
			jLabel1.setBounds(38, 92, 59, 18);
		}
		return jLabel1;
	}

	private JLabel getJLabel0() {
		if (jLabel0 == null) {
			jLabel0 = new JLabel();
			jLabel0.setText("ip：");
			jLabel0.setBounds(40, 58, 41, 18);
		}
		return jLabel0;
	}

	private JRadioButton getJRadioButton1() {
		if (jRadioButton1 == null) {
			jRadioButton1 = new JRadioButton();
			jRadioButton1.setText("加入对战");
			jRadioButton1.setBounds(168, 18, 104, 26);
			jRadioButton1.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					jTextField0.setText("127.0.0.1");
					jTextField0.setEnabled(true);
					jTextField1.setText("8888");
				}
			});
		}
		return jRadioButton1;
	}

	private JRadioButton getJRadioButton0() {
		if (jRadioButton0 == null) {
			jRadioButton0 = new JRadioButton();
			jRadioButton0.setSelected(true);
			jRadioButton0.setText("启动服务器");
			jRadioButton0.setBounds(47, 19, 104, 26);
			jRadioButton0.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					jTextField0.setText("127.0.0.1");
					jTextField0.setEnabled(false);
					jTextField1.setText("8888");
				}
			});
		}
		return jRadioButton0;
	}

	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("确定");
			jButton1.setBounds(39, 134, 70, 28);
			jButton1.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					GameVariable.PORT = Integer.valueOf(jTextField1.getText());
					// 加入游戏
					if (jRadioButton1.isSelected()) {
						/*GameVariable.IP = jTextField0.getText();
						MainFrame.P.remove(MainFrame.m);
						Menu.menu.stop();
						MainFrame.clientPanel = new ClientPanel();
						MainFrame.clientPanel.Action();
						MainFrame.P.add(MainFrame.clientPanel);
						MainFrame.P.setVisible(true);
						MainFrame.P.setFocusable(true);
						MainFrame.clientPanel.requestFocus();
						MainFrame.P.repaint();*/
						frame.dispose();
						return;
					}
					MainFrame.P.remove(MainFrame.m);
					Menu.menu.stop();
					MainFrame.p1 = new GamePanel();
					MainFrame.p1.Action();
					MainFrame.P.add(MainFrame.p1);
					MainFrame.P.setVisible(true);
					MainFrame.P.setFocusable(true);
					MainFrame.p1.requestFocus();
					MainFrame.P.repaint();
					frame.dispose();
				}
			});
		}
		return jButton1;
	}

	public OnlinePanel(JFrame frame) {
		this.frame = frame;
		initComponents();
	}
	
}
